local lianhuan = fk.CreateSkill {

  name = "joyex__lianhuan",

  tags = {  },

}



lianhuan:addEffect(fk.EventPhaseStart, {
  name = "joyex__lianhuan",
  anim_type = "control",
  events = {fk.EventPhaseStart,fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(lianhuan.name) and target == player and player.phase == Player.Play and not player:prohibitUse(Fk:cloneCard("iron_chain"))
    else
      local room = player.room
      local to = room:getPlayerById(data.to)
      if player:hasSkill(lianhuan.name) and target == player and data.card.name == "iron_chain" and (to == player or (to ~= player and not (to.dead or to.chained or to:isKongcheng()))) then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      return room:askForSkillInvoke(player,lianhuan.name,nil,"#joyex__lianhuan")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      U.askForUseVirtualCard(room, player, "iron_chain", nil, lianhuan.name)
    else
      local to = room:getPlayerById(data.to)
      if to == player and not player.dead then
        player:drawCards(1,lianhuan.name)
      else
        if to:isKongcheng() then return false end
        local throw = table.random(to:getCardIds("h"), 1)
        room:throwCard(throw, lianhuan.name, to, player)
      end
    end
  end,
})
lianhuan:addEffect(fk.TargetSpecified, {
  name = "joyex__lianhuan",
  anim_type = "control",
  events = {fk.EventPhaseStart,fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player:hasSkill(lianhuan.name) and target == player and player.phase == Player.Play and not player:prohibitUse(Fk:cloneCard("iron_chain"))
    else
      local room = player.room
      local to = room:getPlayerById(data.to)
      if player:hasSkill(lianhuan.name) and target == player and data.card.name == "iron_chain" and (to == player or (to ~= player and not (to.dead or to.chained or to:isKongcheng()))) then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      return room:askForSkillInvoke(player,lianhuan.name,nil,"#joyex__lianhuan")
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      U.askForUseVirtualCard(room, player, "iron_chain", nil, lianhuan.name)
    else
      local to = room:getPlayerById(data.to)
      if to == player and not player.dead then
        player:drawCards(1,lianhuan.name)
      else
        if to:isKongcheng() then return false end
        local throw = table.random(to:getCardIds("h"), 1)
        room:throwCard(throw, lianhuan.name, to, player)
      end
    end
  end,
})

lianhuan:addEffect("targetmod", {
  name = "#joyex__lianhuan_targetmod",
  extra_target_func = function(self, player, skill, card)
    if card and card.name == "iron_chain"   then
      return 999
    end
  end,
})

return lianhuan